<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        var data = [{
            id: 1,
            name: '家电',
            goods: [{
                id: 11,
                gname: '冰箱'
            }, {
                id: 12,
                gname: '洗衣机'
            }]
        }, {
            id: 2,
            name: '服饰'
        }];

        function getID(json, id) {
            var o = {};
            json.forEach(function(item) {
                //console.log(item);
                if (item.id == id) {
                    //console.log(item);
                    o = item;
                    return item;
                    //我们想要拿到里层的数据
                } else if (item.goods && item.goods.length > 0) {
                    o = getID(item.goods, id);
                }
            });
            return o;
        }
        console.log(getID(data, 1));
        console.log(getID(data, 2));
        console.log(getID(data, 11));
        // getID(data, 1);
        // getID(data, 2);
        // getID(data, 11);
    </script>
</body>

</html>